package com.electronic.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Scanner;

/**
 * 系统工具类
 *
 * @author zyh 
 * ================================
 * Date: 2024/8/15
 * Time: 下午3:20
 * ================================
 */
public class SystemUtil {
    private static final Logger log = LoggerFactory.getLogger(SystemUtil.class);

    // 获取硬盘序列号
    public static String DiskSerialNumber() {
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"wmic", "diskdrive", "get", "serialnumber"});
            Scanner scanner = new Scanner(process.getInputStream()).useDelimiter("\\A");
            String result = scanner.hasNext() ? scanner.next() : "";
            process.waitFor();
            scanner.close();
            String[] lines = result.split("\r\n|\n"); // 支持不同操作系统的换行符

            for (String line : lines) {
                if (!line.trim().isEmpty() && !(line.trim()).equals("SerialNumber")) {
                    return line.trim();
                }
            }
        } catch (Exception e) {
            log.error("获取硬盘序列号失败");
            return null;
        }
        return null;

    }


    //获取物理地址
    public static String getMacAddress() {

        try {
            // 地址返回结果
            List<String> macAddress = new ArrayList<>();
            // 获取所有网络接口
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            // 遍历网络接口
            while (networkInterfaces.hasMoreElements()) {
                // 获取网络接口
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                // 获取mac地址
                byte[] mac = networkInterface.getHardwareAddress();
                // 判断mac地址是否为空
                if (mac != null) {
                    // 拼接
                    StringBuilder sb = new StringBuilder();
                    // 遍历mac地址
                    for (int i = 0; i < mac.length; i++) {
                        // 格式化
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                    // 添加到结果中
                    macAddress.add(sb.toString());
                }
            }
            // 返回结果
            return macAddress.toString();
        } catch (IOException e) {
            log.error("获取Mac地址失败");
            return null;
        }
    }

    /**
     * 获取CPU序列号
     *
     * @return CPU序列号字符串
     */
    public static String getCpuSerialNumber() {

        try {
            // 创建一个新的进程并执行wmic命令
            Process process = Runtime.getRuntime().exec("wmic cpu get processorid");

            // 创建一个缓冲读取器来读取进程的标准输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            // 读取每一行输出
            String line = "";
            for (int i = 0; i < 3; i++) {
                line = reader.readLine();
            }

            // 关闭读取器
            reader.close();

            // 等待进程完成
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                log.info("Successfully retrieved CPU serial number.");
                return line.trim();
            } else {
                log.error("Failed to retrieve CPU serial number.");
            }
        } catch (Exception e) {
            log.error("获取CPU序列号失败");
            e.printStackTrace();
            return null;
        }
        return null;
    }

    /**
     * 获取本机IP地址
     *
     * @return 本机IP地址字符串
     */
    public static String getLocalIpAddress() {

        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            return inetAddress.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return null;
        }
    }


}

